import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../theme/jr.dart';
import '../utils/constant/text_styles.dart';
import 'dialog_data.dart';

class JRDialogWithCheck extends StatefulWidget {
  final TextWithCheckDialogData data;

  const JRDialogWithCheck({Key? key, required this.data}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _JRDialogWithCheckState();
}

class _JRDialogWithCheckState extends State<JRDialogWithCheck> {
  late bool _checked;

  @override
  void initState() {
    super.initState();
    _checked = widget.data.checked;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16.w),
      ),
      width: 280.w,
      padding: EdgeInsets.symmetric(horizontal: 24.w),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(
            height: 24.h,
          ),
          Text(
            widget.data.title,
            maxLines: 1,
            style: JRTextStyles.styleFont16W600.copyWith(
              color: JR.color.mainText,
            ),
          ),
          SizedBox(
            height: 12.h,
          ),
          Text(
            widget.data.desc,
            textAlign: TextAlign.center,
            style: JRTextStyles.styleFont13W400
                .copyWith(color: JR.color.secondaryText),
          ),
          SizedBox(
            height: 24.h,
          ),
          Row(
            children: [
              if (widget.data.negativeBtnText.isNotEmpty)
                Expanded(
                    child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 6),
                  child: CupertinoButton(
                    onPressed: () async {
                      if ((await widget.data.onNegativeBtnPress?.call()) ??
                          true) {
                        Navigator.of(context).pop(false);
                      }
                    },
                    minSize: 0,
                    padding: EdgeInsets.zero,
                    child: Container(
                      height: 40.h,
                      decoration: const ShapeDecoration(
                        color: Color(0xfff6f7f9),
                        shape: StadiumBorder(),
                      ),
                      alignment: Alignment.center,
                      child: Text(
                        widget.data.negativeBtnText,
                        maxLines: 1,
                        style: JRTextStyles.styleFont16W500.copyWith(
                          fontWeight: FontWeight.w400,
                          color: JR.color.secondaryText,
                        ),
                      ),
                    ),
                  ),
                )),
              if (widget.data.positiveBtnText.isNotEmpty)
                Expanded(
                    child: Padding(
                  padding: const EdgeInsets.symmetric(horizontal: 6),
                  child: CupertinoButton(
                    onPressed: () async {
                      widget.data.checkSetter?.call(_checked);
                      if ((await widget.data.onPositiveBtnPress?.call()) ??
                          true) {
                        Navigator.of(context).pop(true);
                      }
                    },
                    minSize: 0,
                    padding: EdgeInsets.zero,
                    child: Container(
                      height: 40.h,
                      decoration: const ShapeDecoration(
                        color: Color(0xffffd400),
                        shape: StadiumBorder(),
                      ),
                      alignment: Alignment.center,
                      child: Text(
                        widget.data.positiveBtnText,
                        style: JRTextStyles.styleFont16W500
                            .copyWith(color: JR.color.mainText),
                      ),
                    ),
                  ),
                )),
            ],
          ),
          SizedBox(
            height: 12.h,
          ),
          GestureDetector(
            onTap: () {
              setState(() {
                _checked = !_checked;
              });
            },
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  width: 16,
                  height: 16,
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                      shape: BoxShape.circle,
                      border: _checked
                          ? null
                          : Border.all(
                              color: const Color(0xFF999999), width: 1),
                      color: _checked
                          ? const Color(0xffffd400)
                          : Colors.transparent),
                  child: _checked
                      ? const Icon(Icons.check_rounded,
                          size: 12, color: Colors.white)
                      : null,
                ),
                const SizedBox(width: 8),
                Text(
                  widget.data.checkDesc,
                  maxLines: 1,
                  style: JRTextStyles.styleFont12W400
                      .copyWith(color: JR.color.secondaryText),
                ),
              ],
            ),
          ),
          SizedBox(
            height: 24.h,
          ),
        ],
      ),
    );
  }
}
